package src.accenture.com.Common;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import src.accenture.com.entity.MblEmployee;

public class CommonUtil {    
//    public static void setEmpIdToSession(String empId) {
//        FacesContext faceContext = FacesContext.getCurrentInstance();
//        ExternalContext ec = faceContext.getExternalContext(); 
//        HttpServletRequest request = (HttpServletRequest) ec.getRequest();
//        HttpSession  session= request.getSession();
//        session.setAttribute("empId", empId);
//    }
//    
//    public static String getEmpIdFromSession() {
//        FacesContext context = FacesContext.getCurrentInstance();
//        ExternalContext ec = context.getExternalContext();
//        HttpSession session = (HttpSession)ec.getSession(true);        
//        return session.getAttribute("empId").toString();
//    }
//    
//    public static void setEmpNameToSession(String empName) {
//        FacesContext faceContext = FacesContext.getCurrentInstance();
//        ExternalContext ec = faceContext.getExternalContext(); 
//        HttpServletRequest request = (HttpServletRequest) ec.getRequest();
//        HttpSession  session= request.getSession();
//        session.setAttribute("empName", empName);
//    }
//    
//    public static String getEmpNameFromSession() {
//        FacesContext context = FacesContext.getCurrentInstance();
//        ExternalContext ec = context.getExternalContext();
//        HttpSession session = (HttpSession)ec.getSession(true);        
//        return session.getAttribute("empName").toString();
//    }
    
    public static void setMblEmployeeToSession(MblEmployee EmployeeLogin) {
        FacesContext faceContext = FacesContext.getCurrentInstance();
        ExternalContext ec = faceContext.getExternalContext(); 
        HttpServletRequest request = (HttpServletRequest) ec.getRequest();
        HttpSession  session= request.getSession();
        session.setAttribute("EmployeeLogin", EmployeeLogin);
    }
    
    public static MblEmployee getMblEmployeeFromSession() {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext ec = context.getExternalContext();
        HttpSession session = (HttpSession)ec.getSession(true);        
        return (MblEmployee) session.getAttribute("EmployeeLogin");
    }
    
    /**
        * Convenience method for resolving a reference to a managed bean by name
        * rather than by expression.
        * @param beanName
        * @return Managed object
        */
       public static Object getManagedBeanValue(String beanName) {
           StringBuffer buff = new StringBuffer("#{");
           buff.append(beanName);
           buff.append("}");
           return resolveExpression(buff.toString());
       }
    
    public static Object resolveExpression(String expression) {
         FacesContext facesContext = FacesContext.getCurrentInstance();
         Application app = facesContext.getApplication();
         ExpressionFactory elFactory = app.getExpressionFactory();
         ELContext elContext = facesContext.getELContext();
         ValueExpression valueExp = elFactory.createValueExpression(elContext, expression, Object.class);
            return valueExp.getValue(elContext);
        }
}
